package com.example.mvvm.viewmodel;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;
import androidx.viewpager2.widget.ViewPager2.OnPageChangeCallback;

import com.example.mvvm.R;
import com.example.mvvm.adapter.VideoPlayerAdapter;
import com.example.mvvm.base.BaseViewHolder;
import com.example.mvvm.base.BaseViewModel;
import com.example.mvvm.base.ResponseMode;
import com.example.mvvm.databinding.AdapterVideoPlayerBinding;
import com.example.mvvm.http.HttpClient;
import com.example.mvvm.mode.LookVideoMode;
import com.example.mvvm.utils.ActivityManageUtils;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.shuyu.gsyvideoplayer.GSYVideoManager;
import com.shuyu.gsyvideoplayer.utils.Debuger;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.disposables.Disposable;

public class VideoPlayerModel extends BaseViewModel {
    List<LookVideoMode> list = new ArrayList<>();
    private VideoPlayerAdapter adapter = new VideoPlayerAdapter(new ArrayList<>(), R.layout.adapter_video_player);
    private int position = -1;

    public VideoPlayerModel(@NonNull Application application) {
        super(application);
        getData(277, 0, true);
    }

    private final MutableLiveData<List<LookVideoMode>> liveData = new MutableLiveData<>();

    private void getData(int id, int way, boolean isRefresh) {
        Observable<ResponseMode<LookVideoMode>> detail = api()
                .getMomentVideoDetail("28f259d6-13e1-4d9c-b456-3ab3ad28a14a", id, way);
        Disposable disposable = network(detail)
                .subscribe(s -> {
                    if (list.size() - position < 5) {
                        getData(s.getData().getId(), 1, false);
                    }
                    if (isRefresh) {
                        list.clear();
                    }
                    list.add(s.getData());
                    liveData.postValue(list);
                }, throwable -> {
                    ActivityManageUtils.getInstance().pop();
                });
        addDisposable(disposable);
    }

    public OnPageChangeCallback changeCallback = new OnPageChangeCallback() {

        @Override
        public void onPageSelected(int position) {
            super.onPageSelected(position);
            int playPosition = GSYVideoManager.instance().getPlayPosition();
            if (playPosition >= 0) {
                //对应的播放列表TAG
                if (GSYVideoManager.instance().getPlayTag().equals("ProgressVideoPlayer" + playPosition)
                        && (position != playPosition)) {
                    playPosition(position, list.get(position));
                }
            }
        }
    };

    public VideoPlayerAdapter getAdapter() {
        if (adapter == null) {
            adapter = new VideoPlayerAdapter(new ArrayList<>(), R.layout.adapter_video_player);
        }
        return adapter;
    }


    public OnRefreshLoadMoreListener moreListener = new OnRefreshLoadMoreListener() {
        @Override
        public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
            int id = list.get(list.size() - 1).getId();
            getData(id, 1, false);
            refreshLayout.finishLoadMore();
        }

        @Override
        public void onRefresh(@NonNull RefreshLayout refreshLayout) {
            getData(6333, 0, true);
            refreshLayout.finishRefresh();
        }
    };

    public void playPosition(int position, LookVideoMode data) {
        BaseViewHolder<AdapterVideoPlayerBinding> viewHolder = adapter.getHolders(position);
        if (viewHolder != null) {
            viewHolder.getBinding().videoPlayer.release();
            viewHolder.getBinding().videoPlayer.startPlayLogic(data.getMp4_info());
        }
        this.position = position;
    }

    public MutableLiveData<List<LookVideoMode>> getLiveData() {
        return liveData;
    }
}
